function AttRoutine(){
	this.form=this.InitForm();
	this.element=this.InitEleUI();
	this.tableday=this.InitDayTable();
	this.tableweek=null;
	this.tablemouth=null;
	this.tabindex=0;
	
}

AttRoutine.prototype.InitEleUI=function(){
	var element;
	var _this=this;
    $("#year").val($("#hidyear").val());
    $("#mouthyear").val($("#hidmouthyear").val());
    $("#weekyear").val($("#hidweekyear").val());

	layui.use('element', function(){
       element=layui.element;
       
       element.on('tab(tabcontent)', function(data){
    	  if(data.index==1){
    		  if(_this.tableweek==null){
    			  _this.tableweek=_this.InitWeekTable();
    		  }  
    	  }
    	  else if(data.index==2){
    		  if(_this.tablemouth==null){
    			  _this.tablemouth=_this.InitMouthTable();
    		  } 
    	  }
    	  element.tabindex=data.index;
    	  _this.tabindex=data.index;
	}); 
	})
	return element;
}

AttRoutine.prototype.ChangeTab=function(index){
	this.element.tabChange('tabcontent', index);
}

AttRoutine.prototype.InitForm=function(){
	var form;
	layui.use('form', function(){
       form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
	   form.render();
	});  
	return form;
}


AttRoutine.prototype.InitDayTable = function() {
	var table;
	var _this=this;
	layui.use('table', function() {
	    table = layui.table;
		table.render({
			elem : '#daylist'
			,height : $(window).height() - 140
			,url : 'get_list' // 数据接口
			,page : true // 开启分页
			,limit : 30
			,id: 'atttroudaylist'
			,method:"post"
			,cols : [ [ // 表头
			           // {field : 'rowNum',title : '序号',width : 80,align : 'center'} ,
			           {field : 'realname',title : '姓名',width : 120,align : 'center'}
			           //, {field : 'orgname',title : '所在单位',width : 150,align : 'center'}
			           , {field : 'year',title : '年',width : 100,align : 'center'}
			           , {field : 'mouth',title : '月份',width : 120,align : 'center'}
			           , {field : 'day',title : '日',width : 80,align : 'center'}
			           , {field : 'routinedes',title : '内容',width : 200,align : 'center',event: 'showDes', style:'cursor: pointer;'}
			           , {field : 'createtime',title : '时间',width : 170,align : 'center'}
			           //, {field : 'remark',title : '备注',width : 150,align : 'center'}
			           , {title : '操作',width : 100,align : 'center',toolbar : '#operate'}
		            ] ]
		    ,request:{
				type:'post'
			}
			,where:{
				year: $("#year").val(),
				mouth:$("#mouth").val(),
				//day:$("#day").val(),
				day:0,
				routinetype:0
				}
		    ,done: function(res, page, count){
		    	
		    	var dataarr=res.data;
		    	$("[data-field='begintime']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			var begin=(new Date(text)).format("yyyy-MM-dd");
		    			$(this).text(begin);
		    		}
               })  
               $("[data-field='endtime']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			var begin=(new Date(text)).format("yyyy-MM-dd");
		    			$(this).text(begin);
		    		}
              })  
               
		    }
		    
		});

		
		 //监听工具条
	    table.on('tool(daylist)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	        layer.msg('ID：'+ data.id + ' 的查看操作');
	      } else if(obj.event === 'del'){
	    	  
	        top.mylayui.confirm('确定要删除吗？',{icon: 0}, function(index,dd){
	        	 top.mylayui.close(index);
	        	 vAttRoutine.deleteentity(data.routineid);
	        });
	      } else if(obj.event === 'edit'){
	    	  vAttRoutine.openadd(1,obj.data.routineid);
	      }
	      else if(obj.event === 'showDes'){
	    	  //top.mylayui.alert(obj.data.routinedes);
	      }
	    });

	    
	});
	
	return table;
}


AttRoutine.prototype.InitWeekTable = function() {
	var table;
	var _this=this;
	layui.use('table', function() {
	    table = layui.table;
		table.render({
			elem : '#weeklist'
			,height : $(window).height() - 140
			,url : 'get_list' // 数据接口
			,page : true // 开启分页
			,limit : 30
			,id: 'atttrouweeklist'
			,method:"post"
			,cols : [ [ // 表头
			            //{field : 'rowNum',title : '序号',width : 80,align : 'center'},
			            {field : 'realname',title : '姓名',width : 120,align : 'center'}
			           //, {field : 'orgname',title : '所在单位',width : 150,align : 'center'}
			           , {field : 'year',title : '年',width : 100,align : 'center'}
			           , {field : 'mouth',title : '月份',width : 120,align : 'center'}
			           , {field : 'week',title : '周',width : 120,align : 'center'}
			           , {field : 'routinedes',title : '内容',width : 200,align : 'center',event: 'showDes', style:'cursor: pointer;'}
			           , {field : 'createtime',title : '时间',width : 170,align : 'center'}
			           //, {field : 'remark',title : '备注',width : 150,align : 'center'}
			           , {title : '操作',width : 100,align : 'center',toolbar : '#operate'}
		            ] ]
		    ,request:{
				type:'post'
			}
			,where:{
				year: $("#weekyear").val(),
				mouth:$("#weekmouth").val(),
				//week:$("#week").val(),
				week:0,
				routinetype:1
				}
		    ,done: function(res, page, count){
		    	
		    	var dataarr=res.data;
		    	$("[data-field='begintime']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			var begin=(new Date(text)).format("yyyy-MM-dd");
		    			$(this).text(begin);
		    		}
               })  
               $("[data-field='endtime']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			var begin=(new Date(text)).format("yyyy-MM-dd");
		    			$(this).text(begin);
		    		}
              })  
               
		    }
		    
		});

		
		 //监听工具条
	    table.on('tool(weeklist)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	        layer.msg('ID：'+ data.id + ' 的查看操作');
	      } else if(obj.event === 'del'){
	    	  
	        top.mylayui.confirm('确定要删除吗？',{icon: 0}, function(index,dd){
	        	 top.mylayui.close(index);
	        	 vAttRoutine.deleteentity(data.routineid);
	        });
	      } else if(obj.event === 'edit'){
	    	  vAttRoutine.openadd(2,obj.data.routineid);
	      }
	      else if(obj.event === 'showDes'){
	    	  //top.mylayui.alert(obj.data.routinedes);
	      }
	    });

	    
	});
	
	return table;
}


AttRoutine.prototype.InitMouthTable = function() {
	var table;
	var _this=this;
	layui.use('table', function() {
	    table = layui.table;
		table.render({
			elem : '#mouthlist'
			,height : $(window).height() - 140
			,url : 'get_list' // 数据接口
			,page : true // 开启分页
			,limit : 30
			,id: 'atttroumouthlist'
			,method:"post"
			,cols : [ [ // 表头
			            {field : 'rowNum',title : '序号',width : 80,align : 'center'}
			           , {field : 'realname',title : '姓名',width : 120,align : 'center'}
			           //, {field : 'orgname',title : '所在单位',width : 150,align : 'center'}
			           , {field : 'year',title : '年',width : 100,align : 'center'}
			           , {field : 'mouth',title : '月份',width : 120,align : 'center'}
			           , {field : 'routinedes',title : '内容',width : 200,align : 'center',event: 'showDes', style:'cursor: pointer;'}
			           , {field : 'createtime',title : '时间',width : 170,align : 'center'}
			           //, {field : 'remark',title : '备注',width : 150,align : 'center'}
			           , {title : '操作',width : 100,align : 'center',toolbar : '#operate'}
		            ] ]
		    ,request:{
				type:'post'
			}
			,where:{
				year: $("#mouthyear").val(),
				//mouth:$("#mouthmouth").val(),
				mouth:0,
				routinetype:2
				}
		    ,done: function(res, page, count){
		    	
		    	var dataarr=res.data;
		    	$("[data-field='begintime']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			var begin=(new Date(text)).format("yyyy-MM-dd");
		    			$(this).text(begin);
		    		}
               })  
               $("[data-field='endtime']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			var begin=(new Date(text)).format("yyyy-MM-dd");
		    			$(this).text(begin);
		    		}
              })  
               
		    }
		    
		});

		
		 //监听工具条
	    table.on('tool(mouthlist)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	        layer.msg('ID：'+ data.id + ' 的查看操作');
	      } else if(obj.event === 'del'){
	    	  
	        top.mylayui.confirm('确定要删除吗？',{icon: 0}, function(index,dd){
	        	 top.mylayui.close(index);
	        	 vAttRoutine.deleteentity(data.routineid);
	        });
	      } else if(obj.event === 'edit'){
	    	  vAttRoutine.openadd(3,obj.data.routineid);
	      }
	      else if(obj.event === 'showDes'){
	    	  //top.mylayui.alert(obj.data.routinedes);
	      }
	    });

	    
	});
	
	return table;
}

AttRoutine.prototype.openadd=function(type,routineid){
	var url="";
	var title='';
	switch(type){
	case 1:
		url="attroutine/att_routine_day_edit";
		title='日工作纪实';
		break;
	case 2:
		url='attroutine/att_routine_week_edit';
		title='周工作总结';
		break;
	case 3:
		url='attroutine/att_routine_mouth_edit';
		title='月工作总结';
		break;
	}
	url+="?routineid="+routineid;
	top.mylayui.open({
         type: 2,
         title: title,
         shadeClose: true,
         shade: 0.5,
         area: ['936px', '440px'],
         content: url
       }); 
	var iframeele=top.$(".layui-layer-content").find('iframe');
	iframeele.css({"height":$(iframeele).height()-10});
}

AttRoutine.prototype.addresult=function(result,msg){
	if(result>0){
		top.mylayui.msg('操作成功', {icon: 1});
		top.mylayui.closeAll('iframe');
		if(vAttRoutine.tabindex==0){
			vAttRoutine.searchdaytable();
		}else if(vAttRoutine.tabindex==1){
			vAttRoutine.searchweektable();
		}else if(vAttRoutine.tabindex==2){
			vAttRoutine.searchmouthtable();
		}
	}
	else{
		if(msg){
			top.mylayui.msg(msg, {icon: 0});
		}else{
			top.mylayui.msg('操作失败', {icon: 0});
		}
	}
}

AttRoutine.prototype.deleteentity=function(routineid){
	$.ajax({
		type:"POST",
		url:"delete_attroutine",
		data:{routineid:routineid},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				//vOrg.searchtable();
				//document.location.reload();
				if(vAttRoutine.tabindex==0){
					vAttRoutine.searchdaytable();
				}else if(vAttRoutine.tabindex==1){
					vAttRoutine.searchweektable();
				}else if(vAttRoutine.tabindex==2){
					vAttRoutine.searchmouthtable();
				}
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}

AttRoutine.prototype.searchdaytable=function(){
	this.tableday.reload('atttroudaylist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    ,where: {  
	    	year: $("#year").val(),
			mouth:$("#mouth").val(),
			day:$("#day").val(),
			routinetype:0
	    }  
	}); 
}

AttRoutine.prototype.searchweektable=function(){
	this.tableweek.reload('atttrouweeklist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    ,where: {  
	    	year: $("#weekyear").val(),
			mouth:$("#weekmouth").val(),
			week:$("#week").val(),
			routinetype:1
	    }  
	}); 
}

AttRoutine.prototype.searchmouthtable=function(){
	this.tablemouth.reload('atttroumouthlist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    ,where: {  
	    	year: $("#mouthyear").val(),
			mouth:$("#mouthmouth").val(),
			routinetype:2
	    }  
	}); 
}

var vAttRoutine=null;

$(function(){
	vAttRoutine=new AttRoutine();
})